package com.byit.controller;


import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.byit.constant.DefaultValue;
import com.byit.domain.SysInterface;
import com.byit.enums.PromptInfo;
import com.byit.log.ChannelType;
import com.byit.log.Log;
import com.byit.log.OperatorType;
import com.byit.msg.MessageResult;
import com.byit.service.ISysInterfaceService;
import com.byit.shrio.session.SessionHelper;
import com.byit.utils.StringUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

/**
 * 接口管理
 *
 * @author tjm
 * @date 2019年1月22日
 */
@RestController
@RequestMapping("/api/sys/interface")
public class SysInterfaceController {

    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private ISysInterfaceService interfaceService;


    /**
     * 添加
     */
    @Log(title = "接口管理", operate = OperatorType.DATA_INSERT, channel = ChannelType.WEB_INTER)
    @PostMapping("/add")
    public Object add(HttpSession session,
                      @RequestParam("name") String name,
                      @RequestParam("describes") String describes,
                      @RequestParam("url") String url,
                      @RequestParam("doc") String doc,
                      @RequestParam("tag") String tag,
                      @RequestParam("type") Integer type,
                      @RequestParam("isOut") Integer isOut) {
        try {
            Long userId = SessionHelper.getUserId(session);
            SysInterface sysInterface = new SysInterface();
            sysInterface.setName(name);
            sysInterface.setDescribes(describes);
            sysInterface.setUrl(url);
            sysInterface.setDoc(doc);
            sysInterface.setTag(tag);
            sysInterface.setType(type);
            sysInterface.setIsOut(isOut);
            sysInterface.setOperationId(userId);
            sysInterface.setCreateTime(new Date());
            sysInterface.setUpdateTime(sysInterface.getCreateTime());
            interfaceService.insert(sysInterface);
            return MessageResult.success();
        } catch (Exception e) {
            logger.error(e.getMessage());
            return MessageResult.error(e);
        }
    }

    /**
     * 修改
     */
    @Log(title = "接口管理", operate = OperatorType.DATA_UPDATE, channel = ChannelType.WEB_INTER)
    @PostMapping("/update")
    public Object update(@RequestParam("id") Long id,HttpSession session,String tag,
                         String name,String describes,String url,String doc,Integer type, Integer isOut) {
        try {
            Long userId = SessionHelper.getUserId(session);
            SysInterface sysInterface = new SysInterface();
            sysInterface.setId(id);
            sysInterface.setName(name);
            sysInterface.setDescribes(describes);
            sysInterface.setTag(tag);
            sysInterface.setUrl(url);
            sysInterface.setDoc(doc);
            sysInterface.setType(type);
            sysInterface.setIsOut(isOut);
            sysInterface.setOperationId(userId);
            sysInterface.setUpdateTime(new Date());
            interfaceService.updateById(sysInterface);
            return MessageResult.success();
        } catch (Exception e) {
            logger.error(e.getMessage());
            return MessageResult.error(e);
        }
    }

    /**
     * 详情
     */
    @Log(title = "接口管理", operate = OperatorType.DATA_SELECT, channel = ChannelType.WEB_INTER)
    @PostMapping("/details")
    public Object details(@RequestParam("id") Long id) {
        SysInterface sysInterface = interfaceService.selectById(id);
        if (sysInterface.getIsDel().equals(1)){
            return  MessageResult.error("该数据已删除");
        }
        return MessageResult.success(sysInterface);
    }

    /**
     * 分页列表查询
     */
    @Log(title = "接口管理", operate = OperatorType.DATA_SELECT, channel = ChannelType.WEB_INTER)
    @GetMapping("/list")
    public Object list(@RequestParam(value = "page", defaultValue = DefaultValue.PAGE_FIRST) int page,
                       @RequestParam(value = "limit", defaultValue = DefaultValue.PAGE_SIZE) int limit,
                       String name,Integer type, Integer isOut,String tag) {
        Page<SysInterface> pageInfo = new Page<>(page, limit);
        EntityWrapper<SysInterface> wrapper = new EntityWrapper<>();
        wrapper.setSqlSelect("id,name,tag,describes,url,doc,type,is_out as isOut,ask_num as askNum,success_num as successNum,create_time as createTime,update_time as updateTime");
        wrapper.eq("is_del",0);
        if (StringUtil.isNotEmpty(name)) {
            wrapper.like("name", name);
        }
        if (StringUtil.isNotEmpty(tag)) {
            wrapper.like("tag", tag);
        }
        if (StringUtil.isNotNull(type)){
            wrapper.eq("type",type);
        }
        if (StringUtil.isNotNull(isOut)){
            wrapper.eq("is_out",isOut);
        }
        wrapper.orderBy("id",false);
        Page<SysInterface> sysInterfacePage = interfaceService.selectPage(pageInfo, wrapper);
        return MessageResult.warpper(PromptInfo.SYS_SECCUSS, sysInterfacePage);
    }

    /**
     * 删除
     */
    @Log(title = "接口管理", operate = OperatorType.DATA_DELETE, channel = ChannelType.WEB_INTER)
    @PostMapping("/del")
    public Object delete(HttpSession session, @RequestParam("ids") String ids) {
        Long userId = SessionHelper.getUserId(session);
        List<Long> idList = StringUtil.splitLong(ids, ",");
        for (Long id : idList) {
            SysInterface sysInterface = new SysInterface();
            sysInterface.setId(id);
            sysInterface.setIsDel(1);
            sysInterface.setUpdateTime(new Date());
            sysInterface.setOperationId(userId);
            interfaceService.updateById(sysInterface);
        }
        return MessageResult.success();
    }

}
